package zp.history;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import zp.history.R;
import zp.history.database.DbAdapter;
import zp.history.util.Log;
import zp.history.util.TagUtil;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.database.Cursor;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.widget.TextView;
import android.widget.Toast;

public class History extends Activity implements OnClickListener {

	private final String DATABASE_PATH = android.os.Environment
			.getExternalStorageDirectory().getAbsolutePath()
			+ "/history_db";

	private final String DATABASE_FILENAME = "history.db";

	private SimpleDateFormat sdf;

	// UI元素
	private TextView view_today;
	private ListView layout_history;
	private TextView view_history_date;
	private TextView view_history_content;
	private TextView view_previous;
	private TextView view_next;
	private ListView list_cotent;
	
	private DbAdapter db;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		Log.open(TagUtil.TAG);
		initShow();

		initDate(getToday());
	}

	private void initShow() {
		view_today = (TextView) findViewById(R.id.today);
		layout_history = (ListView) findViewById(R.id.history_view);
		view_history_date = (TextView) findViewById(R.id.history_date);
		view_history_content = (TextView) findViewById(R.id.history_content);
		view_previous = (TextView) findViewById(R.id.previous);
		view_next = (TextView) findViewById(R.id.next);
		list_cotent = (ListView)findViewById(R.id.history_view);
		list_cotent.setCacheColorHint(0);

		view_previous.setOnClickListener(this);
		view_next.setOnClickListener(this);
		

		sdf = new SimpleDateFormat("yyyyMMdd");

	}

	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.previous:
			initDate(previous(view_today.getText().toString().replace("-", "")));
			break;
		case R.id.next:
			initDate(next(view_today.getText().toString().replace("-", "")));
			break;
		default:
			break;
		}

	}

	private Cursor mCursor;

	private void initDate(String day) {

		view_today.setText(day.substring(0, 4) + "-" + day.substring(4, 6)
				+ "-" + day.substring(6));

		SimpleCursorAdapter adapter = null;

		try {
			db = new DbAdapter(DATABASE_PATH, DATABASE_FILENAME, getResources());
			mCursor = db.getItems(day);
			String[] from = new String[] { DbAdapter.KEY_DATE,
					DbAdapter.KEY_CONTENT };
			int[] to = new int[] { R.id.history_date, R.id.history_content };

			adapter = new SimpleCursorAdapter(this, R.layout.historyitem,
					mCursor, from, to);

		} catch (Exception e) {
			Log.e(e.toString());
			Toast.makeText(this, R.string.error_database, Toast.LENGTH_SHORT);
		}

		layout_history.setAdapter(adapter);

		Log.i("It's OK!");
	}

	private String getToday() {
		// SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
		return sdf.format(new Date());
	}

	/**
	 * previous day
	 * 
	 * @param now
	 * @return
	 */
	private String previous(String now) {
		return roll(now, -1);
	}

	/**
	 * next day
	 * 
	 * @param now
	 * @return
	 */
	private String next(String now) {
		return roll(now, 1);
	}

	/**
	 * roll the date for 'value' days
	 * 
	 * @param now
	 *            date yyyyMMdd
	 * @param value
	 *            n days
	 * @return
	 */
	private String roll(String now, int value) {
		try {
			Date nd = sdf.parse(now);
			Calendar c = Calendar.getInstance();
			c.setTime(nd);
			c.roll(Calendar.DAY_OF_YEAR, value);

			return sdf.format(c.getTime());
		} catch (ParseException e) {
			e.printStackTrace();
			return "error date";
		}

	}

	// 添加MENU
	private static final int MENU_ABOUT = Menu.FIRST;
	private static final int MENU_QUIT = Menu.FIRST + 1;

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		menu.add(0, MENU_ABOUT, 0, R.string.about).setIcon(
				android.R.drawable.ic_menu_help);
		menu.add(0, MENU_QUIT, 0, R.string.quit).setIcon(
				android.R.drawable.ic_menu_close_clear_cancel);
		return super.onCreateOptionsMenu(menu);
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case MENU_ABOUT:
			openAboutDialog();
			break;

		case MENU_QUIT:
			finish();
			break;
		default:
			break;
		}
		return super.onOptionsItemSelected(item);
	}

	public void openAboutDialog() {
		new AlertDialog.Builder(this).setTitle(R.string.about_title)
				.setMessage(R.string.about_content).setPositiveButton(
						R.string.about_button,
						new DialogInterface.OnClickListener() {
							public void onClick(DialogInterface dialog,
									int which) {
							}

						}).show();
	}

	

}